package com.duobeiyun.gmstest.work

import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.duobeiyun.generamessagesdk.ErrorInfo
import com.duobeiyun.generamessagesdk.ResultCallback
import com.duobeiyun.generamessagesdk.channel.GmsChannelListener
import com.duobeiyun.generamessagesdk.channel.bean.GmsChannelAttribute
import com.duobeiyun.generamessagesdk.channel.bean.GmsChannelMember
import com.duobeiyun.generamessagesdk.connect.bean.GmsMessage
import com.duobeiyun.gmstest.TestInstance
import com.duobeiyun.gmstest.log.LogDebug
import com.duobeiyun.gmstest.log.LogOuter
import com.duobeiyun.gmstest.work.sample.WorkAsyncMode
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
 * 加入频道测试
 */
class WorkJoinChannel(appContext: Context, workerParams: WorkerParameters) :
        WorkAsyncMode(appContext, workerParams) {

    override fun setNumber() {
        TAG = "WorkJoinChannel"
        number = "4"
    }
    override fun realWork(continuation: CancellableContinuation<Result>) {

        TestInstance.createChannel(TestInstance.channelId!!, object : GmsChannelListener {
            override fun onAttributesUpdated(attrList: List<GmsChannelAttribute>) {

            }

            override fun onMemberCountUpdated(count: Int) {

            }

            override fun onMemberJoined(member: GmsChannelMember) {

            }

            override fun onMemberLeft(member: GmsChannelMember) {

            }

            override fun onMessageReceived(gmsMessage: GmsMessage, member: GmsChannelMember) {

            }

        }, object : ResultCallback<Void> {
            override fun onSuccess(responseInfo: Void?) {
                LogOuter.print("number:$number \t result:${true} \t name:JoinChannel ")
                Log.e("MainActivity", "join success")
                continuation.resume(Result.success())
            }

            override fun onFailure(errorInfo: ErrorInfo) {
                LogDebug.logE(errorInfo.toString())
                LogOuter.print("number:$number \t result:${false} \t name:JoinChannel ")
                Log.e("MainActivity", "join failure$errorInfo")
                continuation.resume(Result.success())
            }
        })
    }

}